unit fBtype;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxControls, cxContainer, cxEdit, cxGroupBox, cxHint, Menus,
  cxLookAndFeelPainters, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, DB, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, StdCtrls, cxButtons, cxGridExportLink,tools,
  uDAScriptingProvider, uDADataTable, uDAMemDataTable, uDaInterfaces,fCLientDataModule,
  cxLookAndFeels, dxSkinsCore, dxSkinCaramel, dxSkinOffice2010Silver,
  dxSkinSeven, dxSkinscxPCPainter;

  function F2Menu(var F2Rec:TF2Rec):boolean;
  
type
  TfmBtype = class(TForm)
    popExport: TPopupMenu;
    Excel: TMenuItem;
    Html: TMenuItem;
    Xml: TMenuItem;
    Txt: TMenuItem;
    SaveDialog: TSaveDialog;
    cxHintStyleController1: TcxHintStyleController;
    cxGroupBox1: TcxGroupBox;
    btnAll: TcxButton;
    btnCancel: TcxButton;
    btnOk: TcxButton;
    btnExit: TcxButton;
    tv: TcxGridDBTableView;
    lv: TcxGridLevel;
    grd: TcxGrid;
    tvmsystem: TcxGridDBColumn;
    tvmname: TcxGridDBColumn;
    tvmtype: TcxGridDBColumn;
    qry: TDAMemDataTable;
    ds: TDADataSource;
    procedure HtmlClick(Sender: TObject);
    procedure XmlClick(Sender: TObject);
    procedure TxtClick(Sender: TObject);
    procedure btnAllClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnOkClick(Sender: TObject);
    procedure ExcelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    m_F2Rec:TF2Rec;
  end;

var
  fmBtype: TfmBtype;

implementation

{$R *.dfm}
function F2Menu(var F2Rec:TF2Rec):boolean;
var
  bFlag:boolean;
begin
  bFlag:=false;
  fmBtype:=TfmBtype.Create(application);
  fmBtype.m_F2Rec:=F2Rec;
  if fmBtype.ShowModal=mrOk then
  begin
    F2Rec:=fmBtype.m_F2Rec;
    if F2Rec.bMultiSelect then
    begin
      if F2Rec.lpsltRet1.Count=0 then
        bFlag:=false
      else
        bFlag:=true;
    end
    else
    begin
      if F2Rec.strTrans1='' then
        bFlag:=false
      else
        bFlag:=true;
    end;
  end;
  fmBtype.Free;
  Result:=bFlag;
end;

procedure TfmBtype.HtmlClick(Sender: TObject);
begin
    if SaveDialog.Execute then
        ExportGridToHTML(SaveDialog.FileName,TcxGrid(popExport.PopupComponent),true,true,'html');
end;

procedure TfmBtype.XmlClick(Sender: TObject);
begin
    if SaveDialog.Execute then
        ExportGridToXML(SaveDialog.FileName,TcxGrid(popExport.PopupComponent),true,true);
end;

procedure TfmBtype.TxtClick(Sender: TObject);
begin
    if SaveDialog.Execute then
        ExportGridToText(SaveDialog.FileName,TcxGrid(popExport.PopupComponent),true,true,'','','','txt');
end;

procedure TfmBtype.btnAllClick(Sender: TObject);
begin
    tv.DataController.SelectAll;
end;

procedure TfmBtype.btnCancelClick(Sender: TObject);
begin
    tv.DataController.ClearSelection;
end;

procedure TfmBtype.btnExitClick(Sender: TObject);
begin
    ModalResult:=mrCancel;
end;

procedure TfmBtype.FormShow(Sender: TObject);
var
  sql: string;
begin
  if m_F2Rec.bMultiSelect then
  begin
    tv.OptionsSelection.MultiSelect:=true;
    btnAll.Visible:=true;
    btnCancel.Visible:=true;
  end
  else
  begin
    tv.OptionsSelection.MultiSelect:=false;
    btnAll.Visible:=false;
    btnCancel.Visible:=false;
  end;
  sql := 'isnull(bm_type,'''')<>''''';
  qry.Where.Clear;
  qry.Where.AddText(sql,False);
  qry.Open;
end;

procedure TfmBtype.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  qry.Close;
end;

procedure TfmBtype.btnOkClick(Sender: TObject);
var
  dc:TcxCustomDataController;
  ARowIndex:integer;
  ARowInfo:TcxRowInfo;
  I:integer;
begin
  dc:=tv.DataController;
  if m_F2rec.bMultiSelect then
  begin
    with dc do
    begin
    dc.BeginUpdate;
    try
      for I:=0 to dc.GetSelectedCount-1 do
      begin
        ARowIndex := dc.GetSelectedRowIndex(I);
        ARowInfo := dc.GetRowInfo(ARowIndex);
        m_F2rec.lpsltRet1.Add(dc.Values[ARowInfo.RecordIndex,tvmtype.Index]);
        m_F2rec.lpsltRet2.Add(dc.Values[ARowInfo.RecordIndex,tvmname.Index])
      end
    finally
      dc.EndUpdate;
    end;
    end;
  end
  else
  begin
    ARowIndex := dc.GetSelectedRowIndex(0);
    ARowInfo := dc.GetRowInfo(ARowIndex);
    m_F2rec.strTrans1:=dc.Values[ARowInfo.RecordIndex,tvmtype.Index];
  end;
  ModalResult:=mrOk;
end;

procedure TfmBtype.ExcelClick(Sender: TObject);
begin
  if SaveDialog.Execute then
    ExportGridToExcel(SaveDialog.FileName,TcxGrid(popExport.PopupComponent),true,true,true,'xls');

end;

end.
